//直接更新state多个方法的对象
import Vue from 'vue';
import {
  RECEIVE_GOODS,
  RECEIVE_RATINGS,
  RECEIVE_INFO,
  INCREMENT_FOOD_COUNT,
  DECREMENT_FOOD_COUNT,
  CLEAR_CART
} from './mutation-types'

export default {
  [RECEIVE_GOODS](state,{goods}){
    state.goods = goods
  },
  [RECEIVE_INFO](state,{info}){
    state.info = info
  },
  [RECEIVE_RATINGS](state,{ratings}){
    state.ratings = ratings
  },
  [INCREMENT_FOOD_COUNT](state,{food}){
    if(!food.count) { // 第一次点击
      Vue.set(food,'count',1);
      //将food添加到cartFoods 中
      state.cartFoods.push(food)
    }else{
      food.count ++;

    }
  },
  [DECREMENT_FOOD_COUNT](state,{food}){
    if(food.count){ //有值才去减
      food.count--;
      if(food.count===0){
        state.cartFoods.splice(state.cartFoods.indexOf(food),1)
      }
    }

  },
  [CLEAR_CART](state){
    // 清除food中count
    state.cartFoods.forEach(food=>food.count = 0);
    //清除购物车中所有购物项
     state.cartFoods = []
  }

}
